<template>
  <div class="my-page">
    <MyForm v-bind="formItemConfig" @handleSearch="handleSearch" @handle-reset="handleReset" />
    <MyTable
      v-bind="tableConfig"
      :tableData="pageData.data"
      @handleView="handleView"
      @handle-edit="handleEdit"
      @handle-add="handleAdd"
      @handle-del="handleDel"
      :loading="pageData.loading"
      v-model:page="pageData.pageInfo"
      :get-data="getData"
    >
      <template #createDate="scoped"> {{ utcFormat(scoped.row.createAt) }}</template>
      <template #avatar="scoped">
        <el-image preview-teleported style="width: 50px; height: 50px" :src="scoped.row.avatar" fit="cover" />
      </template>
    </MyTable>
    <MyDialog v-bind="dialogFormItemConfig" ref="dialogRef" @handle-opt="handleOpt"></MyDialog>
  </div>
</template>

<script lang="ts" setup>
import MyForm from '@/components/AppForm/index.vue'
import MyTable from '@/components/myTable/index.vue'
import MyDialog from '@/components/AppDialog/index.vue'

import { useDialogHooks } from '@/hooks/useDialog'
import { useGetData } from '@/hooks/useGetData'

import { formItemConfig, tableConfig, dialogFormItemConfig } from './config'
import { utcFormat } from '@/utils/format'
import { getDataList, delData, updateData } from '@/services/main'

//参数和获取数据的函数 重置和查询
const { pageData, getData, handleSearch, handleReset } = useGetData(getDataList)

//查看 编辑  添加
const { dialogRef, handleAdd, handleEdit, handleView } = useDialogHooks()

//删除
const handleDel = (val: any) => {
  delData({ id: val.id }).then((res) => {
    if (res.code === 0) {
      pageData.data = res.data
      pageData.pageInfo.total = res.total
      ElMessage.success('删除成功')
    }
  })
}

//修改
const handleOpt = (params: any, type: string) => {
  if (type === 'edit') {
    updateData(params).then((res) => {
      if (res.code === 0) {
        pageData.data = res.data
        pageData.pageInfo.total = res.total
        ElMessage.success('修改成功')
      }
    })
  }
}
</script>

<style lang="scss" scoped></style>
